app.controller('ProductListController', ['$scope', 'products', function ($scope, products) {
    $scope.listproduct; 
    $scope.mode;
    $scope.Submitted=false;
    $scope.entity = {
    		"name":'',
            "description":'',
            "tax_Amount":0.0, 
             "fg_Allow_Custom_Amount":0.0,
           "fg_Active":false,
             "vlCalcType":0 
    }
    //load list
      products.getList().then(function(result){
    	  $scope.listproduct = result;
    	  
      });
    
    $scope.CreateNew=function(){
    	 $scope.entity = {
    	    		"name":'',
    	            "description":'',
    	            "tax_Amount":0.0, 
    	             "fg_Allow_Custom_Amount":0.0,
    	           "fg_Active":false,
    	             "vlCalcType":0 
    	    }  ;
    	 $scope.mode="New";
    	 $scope.$apply();
    }
    
    $scope.Save=function(isvalid)
    {
    	$scope.Submitted=true;
    	if(isvalid)
    		{
    		if($scope.mode ==="New")
    			{
    		//go to insert	
    				products.insert($scope.entity).then(function(result){
    					
    					$scope.listproduct = result;
    				});
    			}
    		else
    			{
    			//goto update
    			products.edit($scope.entity).then(function(result){
					
    				$scope.listproduct = result;
				});
    			}
    		
    		}
    }
    initTable();
    //inital column and binding list catelog to table.
    function initTable() {

        $scope.aoColumns = [
        { "sTitle": "Name" },
        { "sTitle": "Description" }, 
		{ "sTitle": "Tax Amount" }, 
		{ "sTitle": "Allow Custom Amount" }, 
        {
            "sTitle": "Active",
            "mRender": function (obj) { 
                if (obj) {
                    return 'Active';
                }
                return 'Inactive';
            }
        },
        { "sTitle": "Calculate Type" },
        {
            "sTitle": "",
            "mRender": function (obj) {
              
                var action = "<a href=\"\" onclick=\"angular.element(this).scope().Edit('" + obj + "')\">" +
                                "<i class=\"fa fa-edit\"></i>" +
                            "</a>" +
                            "<a href=\"\" onclick=\"angular.element(this).scope().Delete('" + obj + "')\"><i class=\"fa fa-ban\"></i></a>";

                return action;
            }
        }];

        $scope.aoColumnDefs = [
                            { "mDataProp": "name", "aTargets": [0] },
                            { "mDataProp": "description", "aTargets": [1] },
                            { "mDataProp": "tax_Amount", "aTargets": [2] },
                            { "mDataProp": "fg_Allow_Custom_Amount", "aTargets": [3] },
                            { "mDataProp": "fg_Active", "aTargets": [4] },
                            { "mDataProp": "vlCalcType", "aTargets": [5] },
                            { "mDataProp": "id", "aTargets": [6] }
        ];
    }
    //edit entity
    $scope.Edit = function (id)
    {
        var u = $.grep($scope.listproduct, function (e) { return e.id == id; });
        var entity = u[0];
        if (entity != undefined) {
            $scope.entity = entity;
            $scope.mode ="Update";
            $scope.$apply();
        }
    }

    //delete entity
    $scope.Delete = function (id)
    {
        var u = $.grep($scope.listproduct, function (e) { return e.id == id; });
        var entity = u[0];
        if (entity != undefined) {
            if (confirm("Are you sure you want to delete this item?")) {
                products.del(entity).then(function (result) {
                	 $scope.listproduct = result;
                })
            }
        }
    }
}]);

